
import { _decorator, Component, Node, tween, v3, Tween, UIOpacity, isValid } from 'cc';
import { EventType } from '../GameSpecial/GameEventType';
import { GlobalEnum } from '../GameSpecial/GlobalEnum';
import yyComponent from './yyComponent';
const { ccclass, property } = _decorator;

@ccclass('UIComponent')
export abstract class UIComponent extends yyComponent {
    abstract uiType: GlobalEnum.UI;
    private _isHiding = false;
    //动画参数
    private _opacity = { a: 0 };
    private _uiOpacity: UIOpacity = null;

    init() {
        if (!this._uiOpacity) {
            this._uiOpacity = this.node.getComponent(UIOpacity);
            if (!this._uiOpacity) {
                this._uiOpacity = this.node.addComponent(UIOpacity);
            }
        }
        this.initSub();
        this.onEvents();
    }

    resetAnim() {
        //动画参数
        this._isHiding = false;
        Tween.stopAllByTarget(this._opacity);
        this._opacity.a = 255;
        this._uiOpacity.opacity = this._opacity.a;
    }

    show(d?) {
        this.init();
        this.resetAnim();
        this.node.active = true;
    }

    hide(d?) {
        //隐藏动画
        this.closeAnim();
    }

    closeAnim() {
        if (this._isHiding) return;
        this._isHiding = true;
        Tween.stopAllByTarget(this._opacity);
        tween(this._opacity).to(0.2, { a: 0 }, {
            onUpdate: () => {
                if (isValid(this.node)) {
                    this._uiOpacity.opacity = this._opacity.a;
                }
            }
        }).call(() => {
            this._isHiding = false;
            this.node.active = false;
        }).start();
    }

    onCloseClick() {
        this.emit(EventType.Common.AudioEvent.playClickBtn);
        this.emit(EventType.Common.UIEvent.exit, this.uiType);
    }

}
